package dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据库连接工具类
 */
public class DB {

    // 数据库DRIVER
    // 数据库5.1.49专用
    // private final static String CLASS_DRIVER = "com.mysql.jdbc.Driver";
    private final static String CLASS_DRIVER = "com.mysql.cj.jdbc.Driver";
    // 数据库地址，修改域名、端口、数据库名称
    // 数据库5.1.49专用
    // private final static String DATABASE_URL = "jdbc:mysql://zhuxiaomo.com:3306/student?serverTimezone=GMT%2B8&&characterEncoding=utf-8";
    private final static String DATABASE_URL = "jdbc:mysql://www.910wsy.com:3306/candyapp?serverTimezone=GMT%2B8&&characterEncoding=utf-8";
    // 数据库账号
    private final static String DATABASE_USER = "root";
    // 数据库密码
    private final static String DATABASE_PASSWORD = "root";

    static {
        try {
            Class.forName(CLASS_DRIVER);//加载数据库驱动
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private DB() {
    }

    public static Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(DATABASE_URL, DATABASE_USER, DATABASE_PASSWORD);//连接数据库
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void closeConnection(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void closeStatement(Statement stmt) {
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 执行查询语句
     *
     * @param sql  要执行的sql，可以传入 ? 作为参数替换
     * @param args 任意个参数
     * @return List<Map>，map内为每一行的参数
     */
    public static List<Map<String, Object>> executeQuery(String sql, Object... args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i + 1, args[i]);
            }
            ResultSet resultSet = preparedStatement.executeQuery();
            List<Map<String, Object>> result = new ArrayList<>();
            while (resultSet.next()) {
                Map<String, Object> temp = new HashMap<>();
                int columnCount = resultSet.getMetaData().getColumnCount();
                for (int i = 1; i <= columnCount; i++) {
                    temp.put(resultSet.getMetaData().getColumnLabel(i), resultSet.getObject(i));
                }
                result.add(temp);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            closeStatement(preparedStatement);
            closeConnection(connection);
        }
    }

    /**
     * 执行更新语句
     *
     * @param sql  要执行的sql，可以传入 ? 作为参数替换
     * @param args 参数列表
     * @return 语句影响的数据行数
     */
    public static int executeUpdate(String sql, Object... args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i + 1, args[i]);
            }
            return preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
            closeStatement(preparedStatement);
            closeConnection(connection);
        }
    }

    public static void main(String[] args) {
        List list =DB.executeQuery(" SELECT * FROM `user` u RIGHT JOIN friend f on u.account =f.friendid \n" +
                "WHERE userid = 1533 and friendid LIKE "+ "'%"+ 8 +"%'");
        String tmp =(String) DB.executeQuery("SELECT account FROM `user` WHERE id = 2").get(0).get("account");
        System.out.println(list.size());
    }

}